﻿using System;
using System.Collections.Generic;
using System.Text;
using Brc_PaaS.Common.Model.Basic;
using Microsoft.Extensions.Configuration;
using SqlSugar;

namespace Brc_PaaS.Dal
{
    public class DbContext
    {
        private SqlSugarClient _instance;
        private static readonly object _lock = new object();
        private SqlEntityModel entity;    
        public DbContext(IConfiguration configuration)
        {
            entity = configuration.GetSection("SqlCon").Get<SqlEntityModel>();
        }

        public SqlSugarClient Instance  
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        if (_instance == null)
                        {
                            _instance = new SqlSugarClient(new ConnectionConfig()
                            {
                                ConnectionString = entity.ConnectionString,
                                DbType = (SqlSugar.DbType)entity.DbType,
                                IsAutoCloseConnection = entity.IsAutoCloseConnection,
                                InitKeyType = (SqlSugar.InitKeyType)entity.InitKeyType,
                                IsShardSameThread = true
                            });
                        }
                    }
                }
                return _instance;
            }
        }


    }
}
